package com.supms.patrol.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.supms.patrol.mapper.DailyInspectionOutclassMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supms.patrol.mapper.DailyInspectionMapper;
import com.supms.patrol.domain.DailyInspection;
import com.supms.patrol.service.IDailyInspectionService;

/**
 * 日常巡查记录Service业务层处理
 * 
 * @author LiangHao
 * @date 2022-03-14
 */
@Service
public class DailyInspectionServiceImpl implements IDailyInspectionService 
{
    @Autowired
    private DailyInspectionMapper dailyInspectionMapper;

    @Autowired
    private DailyInspectionOutclassMapper outclassMapper;

    /**
     * 查询日常巡查记录
     * 
     * @param id 日常巡查记录主键
     * @return 日常巡查记录
     */
    @Override
    public DailyInspection selectDailyInspectionById(Long id)
    {
        return dailyInspectionMapper.selectDailyInspectionById(id);
    }

    /**
     * 查询日常巡查记录列表
     * 
     * @param dailyInspection 日常巡查记录
     * @return 日常巡查记录
     */
    @Override
    public List<DailyInspection> selectDailyInspectionList(DailyInspection dailyInspection)
    {
        String operName = SecurityUtils.getUsername();
        dailyInspection.setCreateBy(operName);
        return dailyInspectionMapper.selectDailyInspectionList(dailyInspection);
    }

    /**
     * 新增日常巡查记录
     * 
     * @param dailyInspection 日常巡查记录
     * @return 结果
     */
    @Override
    public int insertDailyInspection(DailyInspection dailyInspection)
    {
        String operName = SecurityUtils.getUsername();
        dailyInspection.setCreateBy(operName);
        dailyInspection.setSupName(dailyInspectionMapper.getSupNameByOperName(operName));
        dailyInspection.setCreateTime(DateUtils.getNowDate());
        return dailyInspectionMapper.insertDailyInspection(dailyInspection);
    }

    /**
     * 修改日常巡查记录
     * 
     * @param dailyInspection 日常巡查记录
     * @return 结果
     */
    @Override
    public int updateDailyInspection(DailyInspection dailyInspection)
    {
        String operName = SecurityUtils.getUsername();
        dailyInspection.setUpdateBy(operName);
        dailyInspection.setUpdateTime(DateUtils.getNowDate());
        return dailyInspectionMapper.updateDailyInspection(dailyInspection);
    }

    /**
     * 批量删除日常巡查记录
     * 
     * @param ids 需要删除的日常巡查记录主键
     * @return 结果
     */
    @Override
    public int deleteDailyInspectionByIds(Long[] ids)
    {
        // 先查询是否有提前下课记录，有则先做提醒
        Long[] outclassIds = outclassMapper.selectOutclassByDIIds(ids);
        if (outclassIds.length > 0) {
            return 5000;
        } else {
            // 判断日常巡查表中是否做了巡查情况记录，有则先删除记录表
            Long[] haveIds = dailyInspectionMapper.selectDailyInspectionEvalFlagByIds(ids);
            if (haveIds.length > 0) {
                dailyInspectionMapper.deleteDailyInspectionEvalByIds(haveIds);
            }
            return dailyInspectionMapper.deleteDailyInspectionByIds(ids);
        }
    }

    /**
     * 删除日常巡查记录信息
     * 
     * @param id 日常巡查记录主键
     * @return 结果
     */
    @Override
    public int deleteDailyInspectionById(Long id)
    {
        return dailyInspectionMapper.deleteDailyInspectionById(id);
    }

    /**
     * 获取日常巡查记录表（数据综合查看表）
     *
     * @param dailyInspection 接收到要查看表的对象
     * @return 表内容及记录内容对象
     */
    @Override
    public DailyInspection getDailyInspectionTable(DailyInspection dailyInspection) {
        String dailyInspectionEvalFlag = dailyInspection.getDailyInspectionEvalFlag();

        // 判断是否已经评价，没有评价的话不查询评价结果
        // "0" 为未评价，"1" 为已评价
        if ("0".equals(dailyInspectionEvalFlag)) {
            return dailyInspectionMapper.getDailyInspectionTable0(dailyInspection);
        } else if ("1".equals(dailyInspectionEvalFlag)) {
            return dailyInspectionMapper.getDailyInspectionTable1(dailyInspection);
        } else return null;
    }
}
